package sc;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.swing.JPanel;

public class PanelControl extends JPanel
{
	private static final long serialVersionUID = 1L;
	private static PanelControl instance = null;
	private final static GridBagConstraints gbc = new GridBagConstraints();
	private static SeperatorPanel onlineSeperator;
	private static SeperatorPanel offlineSeperator;
	private static SeperatorPanel membersSeperator;
	private static SeperatorPanel lordsSeperator;
	private static SeperatorPanel unknownSeperator;
	private static UnknownIPPanel unknownIP;
	private static Information memberinfo;
	private static List<BasePanel> panels;
	private static List<BasePanel> staticpanels;
	
	private PanelControl()
	{
		setSize(200, 400);
	    setLayout(new GridBagLayout());
	    
		gbc.weightx = 1.0;
		gbc.fill = GridBagConstraints.BOTH;
		gbc.gridwidth = GridBagConstraints.REMAINDER;
		
		onlineSeperator = new SeperatorPanel("Online", "0");
		offlineSeperator = new SeperatorPanel("Offline", "6");
		membersSeperator = new SeperatorPanel("Members", "1");
		lordsSeperator = new SeperatorPanel("Lords", "0");
		unknownSeperator = new SeperatorPanel("Unknown", "6");
		unknownIP = new UnknownIPPanel("Unknown IP", "0");
		
		memberinfo = new Information();
		
		panels = new ArrayList<BasePanel>();
		staticpanels = new ArrayList<BasePanel>();
		staticpanels.add(onlineSeperator);
		staticpanels.add(offlineSeperator);
	}
	
	public static PanelControl getInstance()
	{
		if (instance == null)
			instance = new PanelControl();
		return instance;
	}

	public static void update()
	{
		getInstance();
		memberinfo.update();
		resort();
	}
	
	private static void resort()
	{
		panels.clear();
		if (Member.userMember() == null)
		{
			panels.add(unknownIP);
		}
		else
		{
			panels.addAll(staticpanels);
			for (Member member : Member.getAllMembers())
				panels.add(member);
		}
		Collections.sort(panels);
		instance.removeAll();
		for (BasePanel panel : panels)
			instance.add(panel, gbc);
		instance.revalidate();
		instance.repaint();
	}
	
	public static void sortByName()
	{
		staticpanels.clear();
		staticpanels.add(onlineSeperator);
		staticpanels.add(offlineSeperator);
		for (Member member : Member.getAllMembers())
			member.sortByName();
		resort();
	}

	public static void sortByChat()
	{
		staticpanels.clear();
		staticpanels.add(onlineSeperator);
		staticpanels.add(offlineSeperator);
		for (Member member : Member.getAllMembers())
			member.sortByChat();
		resort();
	}

	public static void sortByVent()
	{
		staticpanels.clear();
		staticpanels.add(onlineSeperator);
		staticpanels.add(offlineSeperator);
		for (Member member : Member.getAllMembers())
			member.sortByVent();
		resort();
	}

	public static void sortByRank()
	{
		staticpanels.clear();
		staticpanels.add(membersSeperator);
		staticpanels.add(lordsSeperator);
		staticpanels.add(unknownSeperator);
		for (Member member : Member.getAllMembers())
			member.sortByRank();
		resort();
	}
}
